from django.contrib import admin
from apps.sales_man.models import SalesMan
from apps.sales_man.models import BoundRelation
from apps.sales_man.models import Single
from apps.sales_man.models import ApplicantList
from apps.sales_man.models import IssueContent
from apps.sales_man.models import IssueImg
from apps.sales_man.models import Channel
from apps.sales_man.models import ChannelProduct
from apps.sales_man.models import ChannelShareImg
from apps.sales_man.models import ChannelPopular
from apps.sales_man.models import ChannelMenus


class ChannelMenusAdmin(admin.ModelAdmin):
    list_display = ("id", "name", "sign", "channel_name")

    def channel_name(self, model):
        return model.channel.name

    channel_name.short_description = "渠道名称"


class ChannelPopularAdmin(admin.ModelAdmin):
    list_display = ("channel_name",)

    def channel_name(self, model):
        return model.channel.name

    channel_name.short_description = "渠道名称"


class ChannelProductInline(admin.TabularInline):
    model = ChannelProduct
    extra = 1  # 用于添加外键关系模型的空行
    can_delete = True  # 是否显示删除框
    show_change_link = True
    fk_name = "channel"


class ChannelShareImgInline(admin.TabularInline):
    model = ChannelShareImg
    extra = 1
    can_delete = True


class ChannelAdmin(admin.ModelAdmin):
    list_display = ("id", "name",)
    search_fields = ("name",)
    inlines = [ChannelProductInline, ChannelShareImgInline]


class ChannelProductAdmin(admin.ModelAdmin):
    list_display = ("channel_name", "product_name", "status")

    def channel_name(self, model):
        return model.channel.name

    def product_name(self, model):
        return model.product.name

    channel_name.short_description = "渠道"
    product_name.short_description = "产品"


class ApplicantListAdmin(admin.ModelAdmin):
    list_display = ("name", "card", "tel", "salesman_name")

    def salesman_name(self, model):
        return model.salesman.name

    salesman_name.short_description = "业务员"


class SingleAdmin(admin.ModelAdmin):
    list_display = ("product_name", "applicant_name", "t_user_name", "create_time", "b_user_name")

    search_fields = ("create_time",)

    def product_name(self, model):
        return model.product.name

    def applicant_name(self, model):
        if model.applicant:
            return model.applicant.name
        else:
            return ""

    def t_user_name(self, model):
        if model.t_user:
            if model.t_user.name:
                return model.t_user.name
            else:
                return model.t_user.account
        else:
            return ""

    def b_user_name(self, model):
        if model.b_user:
            if model.b_user.name:
                return model.b_user.name
            else:
                return model.b_user.account
        else:
            return ""

    product_name.short_description = "产品名字"
    t_user_name.short_description = "推荐人名字"
    applicant_name.short_description = "申请人姓名"
    b_user_name.short_description = "被推荐名字"


class SalesManAdmin(admin.ModelAdmin):
    # inlines = [SalesManExtendInline]
    list_display = ("id", "name", 'tel', "leader", "card_num", "wx_user_openid", "account")
    search_fields = ('tel', "name", "account")

    def leader(self, model):
        return model.leader.name

    def wx_user_openid(self, model):
        if model.wx_user:
            return "%s: %s" % (model.wx_user.nick_name, model.wx_user.openid)
        else:
            return ""

    leader.short_description = "上级姓名"
    wx_user_openid.short_description = "微信授权"


class BoundRelationAdmin(admin.ModelAdmin):
    list_display = ("t_user_name", 'openid', "create_time")

    def t_user_name(self, model):
        return model.t_user.name

    t_user_name.short_description = "推荐用户"


class IssueInline(admin.TabularInline):
    model = IssueImg
    extra = 1  # 用于添加外键关系模型的空行
    can_delete = True  # 是否显示删除框


class IssueContentAdmin(admin.ModelAdmin):
    inlines = [IssueInline]
    list_display = ("title", 'num', "create_time")
    search_fields = ("create_time", "title")


class IssueImgAdmin(admin.ModelAdmin):
    list_display = ("content_name", 'order')

    def content_name(self, model):
        return model.content.title

    content_name.short_description = "标题名"


admin.site.register(Channel, ChannelAdmin)
admin.site.register(ChannelProduct, ChannelProductAdmin)
admin.site.register(SalesMan, SalesManAdmin)
admin.site.register(IssueContent, IssueContentAdmin)
admin.site.register(ApplicantList, ApplicantListAdmin)
admin.site.register(BoundRelation, BoundRelationAdmin)
admin.site.register(Single, SingleAdmin)
admin.site.register(IssueImg, IssueImgAdmin)
admin.site.register(ChannelPopular, ChannelPopularAdmin)
admin.site.register(ChannelMenus, ChannelMenusAdmin)
